﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Physio10Context;
using System.Globalization;
using System.IO;
using System.Web;


namespace Physio10Context
{

    public partial class Booking
    {
        public decimal TotalPrice
        {
            get
            {
               
               Drugorder  order = DatabaseManager.PersistentManager.Instance.GetDrugOrderbyBook(this.BookId);

               if (order == null)
                   return 0;
               return order.AllTotalPrice;

            }
        }

        public string OrderNumber
        {
            get
            {
                Drugorder order = DatabaseManager.PersistentManager.Instance.GetDrugOrderbyBook(this.BookId);
                if (order == null)
                    return "N/A";
                return order.GetOrderNumber();
            }
        }

        public string CustomerName
        {
            get
            {
                return this.Qf8soUser.Name.ToString();
            }
        }

        public string CustomerUserName
        {
            get
            {
                if (Qf8soUser == null)
                    return "";
                return this.Qf8soUser.Username.ToString();
            }
        }
    }

    public partial class Opendate
    {
        public string DoctorName
        {
            get
            {
                return this.Doctor.DoctorName;
            }
        }

        public string DayOfweekName
        {
            get
            {
                return ((DayOfWeek)Convert.ToInt32(DayOfWeek)).ToString();
            }
        }

        public string OpenStatusName
        {
            get
            {
                if (OpenStatus == 1)
                {
                    return "เปิดทำการ";
                }
                else
                    return "ปิดทำการ";
            }
        }
    }

    public partial class Drug
    {
        public void IncreaseAmount()
        {
            this.Amount++;
        }

        public void DecreaseAmount()
        {
            if(this.Amount > 0)
                this.Amount--;
        }

        public void UpdateAmount(int Amount)
        {
            this.Amount = Amount;
        }
    }

    public partial class Doctor
    {
        public string ImageDoctor
        {
            get
            {
                return "./DoctorImage/"+ DoctorId.ToString() + ".jpg";
            }
        }

        public string WorkingDateString
        {
            get
            {
                return ((DayOfWeek)WorkDay).ToString();
            }
        }
    }

    public partial class Drugorder
    {
        public int GetOrderCount()
        {
            return this.Drugorderdetails.Count;
        }
        public decimal ReEvalTotalPrice()
        {
            decimal totalPrice = 0;

            foreach (Drugorderdetail item in this.Drugorderdetails)
            {
                totalPrice += item.Price;
            }

            //TotalPrice = (long)totalPrice;

            return totalPrice;

           // DatabaseManager.PersistentManager.Instance.Save();
        }

        public string GetOrderNumber()
        {
            string orderNumber = this._OrderId.ToString("0000");

            return orderNumber;
        }

        public string GetTaxNumber()
        {
            CultureInfo th = System.Globalization.CultureInfo.GetCultureInfo("th-TH");

            DateTime year = new DateTime(DateTime.Today.Year+543, 1, 1);

            DateTime dt = new DateTime(year.Year, 1, 1, 0, 0, 0, th.Calendar);

            DateTime yearNumber = dt;

            int order_id = Convert.ToInt32(yearNumber.ToString("yy", new CultureInfo("th-TH"))) * 10000;

            order_id += (int)(this.OrderId);

            return order_id.ToString();

        }

        public List<Drugorderdetail> DrugDetailList
        {
            get
            {
                List<Drugorderdetail> lst = new List<Drugorderdetail>();

                foreach (Drugorderdetail item in this.Drugorderdetails)
                {
                    lst.Add(item);
                }


                return lst;
            }
        }

        public decimal PriceAfterDiscount
        {
            get
            {
                decimal discount = 0;
                decimal totalPrice = ReEvalTotalPrice();
                if (Discount != "" && Discount.Last() == '%')
                {
                    discount = decimal.Parse(Discount.TrimEnd(new char[] { '%', ' ' })) / 100M;
                    return totalPrice  * (1 - discount);
                }
                else if (Discount != "")
                {
                    discount = decimal.Parse(Discount);
                    return totalPrice  - discount;
                }
                return totalPrice;
            }
        }

        public decimal Vat
        {
            get
            {
                return PriceAfterDiscount * (decimal)0.07;
            }
        }

        public decimal AllTotalPrice
        {
            get
            {
                return PriceAfterDiscount + Vat;
            }
        }

        public DateTime Appointment
        {
            get
            {
                Booking booking = DatabaseManager.PersistentManager.Instance.GetBooking((int)this.BookId);

                return (DateTime)booking.AppointmentTime;
            }
        }

        public string Customer
        {
            get
            {
                Booking booking = DatabaseManager.PersistentManager.Instance.GetBooking((int)this.BookId);

                return booking.CustomerName;
            }
        }
    }

    public partial class Drugorderdetail
    {
        public string ProductName
        {
            get
            {
                if(this.Drug != null) 
                return this.Drug.DrugName;

                return "";

            }
        }

        public decimal Price
        {
            get
            {
                decimal discount = 0;
                if (Discount != "" && Discount.Last() == '%')
                {
                    discount = decimal.Parse(Discount.TrimEnd(new char[] { '%', ' ' })) / 100M;
                    return (int)this.Drug.DrugPrice * (int)this.Amount * (1 - discount);
                }
                else if (Discount != "")
                {
                    discount = decimal.Parse(Discount);
                    return (int)this.Drug.DrugPrice * (int)this.Amount - (discount*(int)this.Amount);
                }
                return (int)this.Drug.DrugPrice * (int)this.Amount;
            }
        }

        public int unit
        {
            get
            {
                return (int)this.Drug.DrugPrice;
            }
        }



    }
}

namespace DatabaseManager
{
    
    public class PersistentManager
    {
        private Physio10DataContext m_db = null;
        static PersistentManager instance = null;

        PersistentManager()
        {
            m_db = new Physio10DataContext();
        }

        public static PersistentManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new PersistentManager();
                }

                return instance;
            }
        }

        public IQueryable GetAllUsers()
        {
            return m_db.Qf8soUsers;
        }

        public IQueryable GetAllDrugs()
        {
            return m_db.Drugs;
        }

        public void Add(Qf8soUser user)
        {
            m_db.Qf8soUsers.InsertOnSubmit(user);
        }

        public void Add(Drug drug, String user)
        {
            m_db.Drugs.InsertOnSubmit(drug);
            WriteToLogFile(drug, "ADD", user);
        }

        public void Update(Drug drug, String user)
        {
            Drug drug_up = GetDrug(drug.DrugId);

            drug_up.Amount = drug.Amount;
            WriteToLogFile(drug, "UPDATE", user);            

        }

        public void Update(Booking booking)
        {
            Booking booking_up = GetBooking(booking.BookId);
            booking_up.DoctorResultPicture = booking.DoctorResultPicture;
        }

        public void Add(Booking booking)
        {
            m_db.Bookings.InsertOnSubmit(booking);
        }

        public void Add(Drugorder drugOrder, String user)
        {
            m_db.Drugorders.InsertOnSubmit(drugOrder);
            WriteToLogFile(drugOrder, "ADD", user);
        }

        public void Add(Drugorderdetail orderdetail, String user)
        {
            m_db.Drugorderdetails.InsertOnSubmit(orderdetail);
            WriteToLogFile(orderdetail, "ADD", user);
        }

        public void Add(Doctor doctor)
        {
            m_db.Doctors.InsertOnSubmit(doctor);
        }

        public void Delete(Doctor doctor)
        {
            m_db.Doctors.DeleteOnSubmit(doctor);
        }

        public void Delete(Drug drug, String user)
        {
            m_db.Drugs.DeleteOnSubmit(drug);
            WriteToLogFile(drug, "DELETE", user);
        }

        public void Delete(Drugorderdetail orderdetail, String user)
        {
            m_db.Drugorderdetails.DeleteOnSubmit(orderdetail);
            WriteToLogFile(orderdetail, "DELETE", user);
        }

        public void Delete(Drugorder order, String user)
        {
            m_db.Drugorders.DeleteOnSubmit(order);
            WriteToLogFile(order, "DELETE", user);
        }

        public void Delete(Booking booking)
        {
            m_db.Bookings.DeleteOnSubmit(booking);
        }

        public Qf8soUser GetUser(int userId)
        {
            return m_db.Qf8soUsers.SingleOrDefault(d => d.Id == userId);
        }


        public Qf8soUser GetUser(string userName)
        {
            return m_db.Qf8soUsers.SingleOrDefault(d => d.Username == userName);
        }

        public Drug GetDrug(int drugId)
        {
            return m_db.Drugs.SingleOrDefault(d => d.DrugId == drugId);
        }

        public Drug GetDrug(string drugId)
        {
            return m_db.Drugs.SingleOrDefault(d => d.DrugCode == drugId);
        }

        public Opendate GetOpenDate(int id)
        {
            return m_db.Opendates.SingleOrDefault(d => d.Id == id);
        }

        public Opendate GetOpenDatebyDayofWeek(DayOfWeek dayofWeek)
        {
            return m_db.Opendates.SingleOrDefault(d => d.Id == (int)(dayofWeek+1));
        }

        public Drugorder GetDrugOrderbyBook(int bookId)
        {
            return m_db.Drugorders.SingleOrDefault(d => d.BookId == bookId);
        }

        public Doctor GetDoctor(int doctorId)
        {
            return m_db.Doctors.SingleOrDefault(d => d.DoctorId == doctorId);
        }

        public Drugorderdetail GetDrugOrderDetail(int orderDetail)
        {
            return m_db.Drugorderdetails.SingleOrDefault(d => d.DrugOrderDetail == orderDetail);
        }

        public List<Drug> GetAllMedicalService()
        {
            return m_db.Drugs.Where(d => d.IsDrug == false).ToList<Drug>();
        }


        public Qf8soUser GetLogin(string userName, string password)
        {
            return m_db.Qf8soUsers.SingleOrDefault(d => d.Username == userName && d.Password == password);
        }

        public Booking GetBooking(int bookingId)
        {
            return m_db.Bookings.SingleOrDefault(d => d.BookId == bookingId);
        }

        public Bookingtime GetBookingTime(int bookingId)
        {
            return m_db.Bookingtimes.SingleOrDefault(d => d.BookingTimeId == bookingId);
        }

        public string DoctorWorking(DayOfWeek dayofweek)
        {
            //IQueryable<Doctor> doc_list = m_db.Doctors.Where(d => d.WorkDay == (int)dayofweek);
            Opendate date = this.GetOpenDatebyDayofWeek(dayofweek);


            if (date != null)
            {
                return date.DoctorName;
            }
            else
            {
                return "N/A";
            }
        }

        public void GenerateBookingTime(DateTime day)
        {
           List<Bookingtime> booktimes =   m_db.Bookingtimes.Where(d => d.Date == day.Date).ToList<Bookingtime>();
           Bookingtime bookTime = null;

           Opendate openDate =  PersistentManager.Instance.GetOpenDatebyDayofWeek(day.DayOfWeek);

           if (openDate.OpenStatus != 1)
           {
               return;
           }


           if (booktimes.Count == 0)
           {
               if (day.DayOfWeek != DayOfWeek.Saturday && day.DayOfWeek != DayOfWeek.Sunday)
               {
                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(18);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "18:00-18:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(18).AddMinutes(30);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "18:30-19:00";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(19);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "19:00-19:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(19).AddMinutes(30);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "19:30-20:00";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(20);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "20:00-20:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();


                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(20).AddMinutes(30);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "20:30-21:00";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();


               }
               else if (day.DayOfWeek == DayOfWeek.Saturday)
               {
                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(10);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "10:00-10:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(10).AddMinutes(30);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "10:30-11:00";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(11);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "11:00-11:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(11).AddMinutes(30);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "11:30-12:00";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(12).AddMinutes(30);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "12:30-13:00";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(13);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "13:00-13:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();


                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(13).AddMinutes(30);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "13:30-14:00";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(14);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "14:00-14:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(15);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "15:00-15:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(15).AddMinutes(30);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "15:30-16:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(16);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "16:00-16:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(17);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "17:00-17:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(17).AddMinutes(30);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "17:30-18:00";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(18);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "18:00-18:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(18).AddMinutes(30);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "18:30-19:00";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(19);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "19:00-19:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(19).AddMinutes(30);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "19:30-20:00";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(20);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "20:00-20:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();


                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(20).AddMinutes(30);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "20:30-21:00";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();
               }
               else if (day.DayOfWeek == DayOfWeek.Sunday)
               {
                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(10);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "10:00-10:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(10).AddMinutes(30);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "10:30-11:00";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(11);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "11:00-11:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(11).AddMinutes(30);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "11:30-12:00";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(12).AddMinutes(30);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "12:30-13:00";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(13);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "13:00-13:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();


                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(13).AddMinutes(30);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "13:30-14:00";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(14);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "14:00-14:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(15);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "15:00-15:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(15).AddMinutes(30);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "15:30-16:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(16);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "16:00-16:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(17);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "17:00-17:30";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();

                   bookTime = new Bookingtime();
                   bookTime.Date = day;
                   bookTime.Available = true;
                   bookTime.StartTime = day.Date.AddHours(17).AddMinutes(30);
                   bookTime.EndTime = bookTime.StartTime.Value.AddMinutes(30);
                   bookTime.TimeText = "17:30-18:00";
                   m_db.Bookingtimes.InsertOnSubmit(bookTime);
                   Save();
               }
           }

           
        }


        //Persistence
        public void Save()
        {
            m_db.SubmitChanges();

            //m_db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, m_db.Bookings);
            //m_db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, m_db.Drugs);
        }

        public void WriteToLogFile(Drugorderdetail orderdetail, String type, String user)
        {
            DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss", new CultureInfo("en-US"));

            //StreamWriter StrWer = default(StreamWriter);
            //string path = @"C:\Users\suki\Documents\Visual Studio 2010\Projects\physio10\Physio_Clinic\Physio_Clinic\Log\log.txt";
            string path = @"D:\webspace\physio10.com\Log\log.txt";
            //@"\Log\log.txt";
            try
            {
                FileStream aFile = new FileStream(path, FileMode.Append, FileAccess.Write);
                //TextWriter writer = new StreamWriter(path);
                StreamWriter writer = new StreamWriter(aFile);
                //TextWriter writer = new StreamWriter(path);
                //StrWer = File.CreateText(Server.MapPath("MyFiles/") + "thaicreate.txt");
                writer.Write(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss", new CultureInfo("en-US")));
                writer.Write(" : ");
                writer.Write(type);
                writer.Write(" ");
                writer.Write("Username = ");
                writer.Write(user);
                writer.Write(" ");
                writer.Write("DrugOrderId = ");
                writer.Write(orderdetail.DrugOrderId);
                writer.Write(" ");
                writer.Write("Amount = ");
                writer.Write(orderdetail.Amount);
                writer.Write(" ");
                writer.Write("DrugId = ");
                writer.Write(orderdetail.DrugId);
                writer.Write(" ");
                writer.Write("Discount = ");
                writer.WriteLine(orderdetail.Discount);
                              
                writer.Close();
                aFile.Close();
            }
            catch (Exception ex)
            {

            }
        }

        public void WriteToLogFile(Drugorder drugorder, String type, String user)
        {           
            //string path = @"C:\Users\suki\Documents\Visual Studio 2010\Projects\physio10\Physio_Clinic\Physio_Clinic\Log\log.txt";
            string path = @"D:\webspace\physio10.com\Log\log.txt";
            //StreamWriter StrWer = new StreamWriter(path);
            //@"\App_Data\urls.txt";
            //@"\Log\log.txt";
            try
            {
                FileStream aFile = new FileStream(path, FileMode.Append, FileAccess.Write);
                //TextWriter writer = new StreamWriter(path);
                StreamWriter writer = new StreamWriter(aFile);
                //StrWer = File.CreateText(Server.MapPath("MyFiles/") + "thaicreate.txt");
                writer.Write(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss", new CultureInfo("en-US")));
                writer.Write(" : ");
                writer.Write(type);
                writer.Write(" ");
                writer.Write("Username = ");
                writer.Write(user);
                writer.Write(" ");
                writer.Write("OrderId = ");
                writer.Write(drugorder.OrderId);
                writer.Write(" ");
                writer.Write("BookId = ");
                writer.Write(drugorder.BookId);
                writer.Write(" ");
                writer.Write("TotalPrice = ");
                writer.Write(drugorder.TotalPrice);
                writer.Write(" ");
                writer.Write("Discount = ");
                writer.Write(drugorder.Discount);
                writer.Write(" ");
                writer.Write("TaxNumber = ");
                writer.Write(drugorder.TaxNumber);
                writer.Write(" ");
                writer.Write("OrderNumber = ");
                writer.Write(drugorder.OrderNumber);
                writer.Write(" ");
                writer.Write("DateDefineProduct = ");
                writer.WriteLine(drugorder.DateDefineProduct);

                writer.Close();
                aFile.Close();
            }
            catch (Exception ex)
            {

            }
        }

        public void WriteToLogFile(Drug drug, String type, String user)
        {
            //string path = @"C:\Users\suki\Documents\Visual Studio 2010\Projects\physio10\Physio_Clinic\Physio_Clinic\Log\log.txt";
            string path = @"D:\webspace\physio10.com\Log\log.txt";
            //StreamWriter StrWer = new StreamWriter(path);
            //@"\App_Data\urls.txt";
            //@"\Log\log.txt";
            try
            {
                FileStream aFile = new FileStream(path, FileMode.Append, FileAccess.Write);
                //TextWriter writer = new StreamWriter(path);
                StreamWriter writer = new StreamWriter(aFile);
                //StrWer = File.CreateText(Server.MapPath("MyFiles/") + "thaicreate.txt");
                writer.Write(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss", new CultureInfo("en-US")));
                writer.Write(" : ");
                writer.Write(type);
                writer.Write(" ");
                writer.Write("Username = ");
                writer.Write(user);
                writer.Write(" ");
                writer.Write("DrugCode = ");
                writer.Write(drug.DrugCode);
                writer.Write(" ");
                writer.Write("DrugId = ");
                writer.Write(drug.DrugId);
                writer.Write(" ");
                writer.Write("DrugPrice = ");
                writer.Write(drug.DrugPrice);
                writer.Write(" ");
                writer.Write("DrugName = ");
                writer.Write(drug.DrugName);
                writer.Write(" ");
                writer.Write("Amount = ");
                writer.Write(drug.Amount);
                writer.Write(" ");
                writer.Write("DrugDetail = ");
                writer.Write(drug.DrugDetail);

                writer.Close();
                aFile.Close();
            }
            catch (Exception ex)
            {

            }
        }
    }
}
